


let Shader = require('../shader');

let shader_uitls = {

    _spriteShader:null,
    _colorSpriteShader:null,

    initShaders(gl, vshaderSource, fshaderSource) {
        let program = this._createProgram(gl, vshaderSource, fshaderSource);
        if (!program) {
            console.log('Failed to create program');
            return false;
        }

        gl.useProgram(program);
        return new Shader(gl,program);
    },

    _createProgram(gl, vshader, fshader) {
        // Create shader object
        let vertexShader = this._loadShader(gl, gl.VERTEX_SHADER, vshader);
        let fragmentShader = this._loadShader(gl, gl.FRAGMENT_SHADER, fshader);
        if (!vertexShader || !fragmentShader) {
            return null;
        }

        // Create a program object
        let program = gl.createProgram();
        if (!program) {
            return null;
        }

        // Attach the shader objects
        gl.attachShader(program, vertexShader);
        gl.attachShader(program, fragmentShader);

        // Link the program object
        gl.linkProgram(program);

        // Check the result of linking
        let linked = gl.getProgramParameter(program, gl.LINK_STATUS);
        if (!linked) {
            let error = gl.getProgramInfoLog(program);
            console.log('Failed to link program: ' + error);
            gl.deleteProgram(program);
            gl.deleteShader(fragmentShader);
            gl.deleteShader(vertexShader);
            return null;
        }
        return program;
    },

    _loadShader(gl, type, source) {
        // Create shader object
        let shader = gl.createShader(type);
        if (shader == null) {
            console.log('unable to create shader');
            return null;
        }

        // Set the shader program
        gl.shaderSource(shader, source);

        // Compile the shader
        gl.compileShader(shader);

        // Check the result of compilation
        let compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
        if (!compiled) {
            let error = gl.getShaderInfoLog(shader);
            console.log('Failed to compile shader: ' + error);
            gl.deleteShader(shader);
            return null;
        }

        return shader;
    },

    // init_sprite_shader(){
    //     let gl = window.app.gl;
    //
    //     let vs = loader.getShaderCode('sprite_vs.glsl');
    //     let fs = loader.getShaderCode('sprite_fs.glsl');
    //     this._spriteShader = this.initShaders(gl,vs,fs);
    //     this._spriteShader._name = 'sprite_shader';
    //     this._spriteShader.bindAttrs([
    //         ['posHandle','a_position'],
    //         ['texPosHandle','a_texCoord'],
    //     ]);
    //     this._spriteShader.bindUniforms([
    //         ['mvpHandle','a_mvpMatrix'],
    //         ['sampler0Handle','u_sample0'],
    //     ]);
    // },
    //
    // init_colorSprite_shader(){
    //     let gl = window.app.gl;
    //
    //     let vs = loader.getShaderCode('color_sprite_vs.glsl');
    //     let fs = loader.getShaderCode('color_sprite_fs.glsl');
    //
    //     this._colorSpriteShader = this.initShaders(gl,vs,fs);
    //     this._colorSpriteShader._name = 'sprite_color_shader';
    //     this._colorSpriteShader.bindAttrs([
    //         ['posHandle','a_position'],
    //     ]);
    //     this._colorSpriteShader.bindUniforms([
    //         ['mvpHandle','a_mvpMatrix'],
    //         ['colorHandle','u_color']
    //     ]);
    // },
    //
    // getSpriteShader(){
    //     return this._spriteShader;
    // },
    //
    // getColorSpriteShader(){
    //     return this._colorSpriteShader;
    // }
};

module.exports = shader_uitls;